Make it possible to run valgrind on code linked with the libxc libraries.
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Tue, 22 Nov 2005 15:31:16 +0000 (16:31 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Tue, 22 Nov 2005 15:31:16 +0000 (16:31 +0100)
Valgrind cannot see when a hypercall has initialised entries in a data
structure, so appropriate memsets have been placed before using dom0_op_t,
privcmd_hypercall_t, and a couple of miscellaneous blocks passed into this
layer.  This initialises the block so that valgrind considers it to be valid,
but the data therein will be immediately overwritten by the hypercall, all
being well.

These changes are semantically neutral if -DVALGRIND is not set.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
14 files changed:
tools/libxc/Makefile
tools/libxc/xc_bvtsched.c
tools/libxc/xc_domain.c
tools/libxc/xc_evtchn.c
tools/libxc/xc_gnttab.c
tools/libxc/xc_linux_build.c
tools/libxc/xc_linux_restore.c
tools/libxc/xc_misc.c
tools/libxc/xc_private.c
tools/libxc/xc_private.h
tools/libxc/xc_ptrace.c
tools/libxc/xc_sedf.c
tools/libxc/xc_tbuf.c
tools/libxc/xg_private.h

index 2f6f58194233596223534d3815a89d489352922d..08eb0c42a0f3bd755563615484d3a13f2714ef26 100644 (file)
@@ -49,6 +49,11 @@ CFLAGS   += -Werror
 CFLAGS   += -O3
 CFLAGS   += -fno-strict-aliasing
 CFLAGS   += $(INCLUDES) -I.
+
+# Define this to make it possible to run valgrind on code linked with these
+# libraries.
+#CFLAGS   += -DVALGRIND -O0 -ggdb3
+
 # Get gcc to generate the dependencies for us.
 CFLAGS   += -Wp,-MD,.$(@F).d
 LDFLAGS  += -L.
index fc43383dc40c867a634754f0dbb47cd1c3159cc9..a7ab88fc4b3db3bebf9cbe897f1a1ad8ce035506 100644 (file)
@@ -11,7 +11,7 @@
 int xc_bvtsched_global_set(int xc_handle,
                            unsigned long ctx_allow)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
 
     op.cmd = DOM0_SCHEDCTL;
     op.u.schedctl.sched_id = SCHED_BVT;
@@ -24,7 +24,7 @@ int xc_bvtsched_global_set(int xc_handle,
 int xc_bvtsched_global_get(int xc_handle,
                            unsigned long *ctx_allow)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     int ret;
     
     op.cmd = DOM0_SCHEDCTL;
@@ -46,7 +46,7 @@ int xc_bvtsched_domain_set(int xc_handle,
                            long long warpl,
                            long long warpu)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     struct bvt_adjdom *bvtadj = &op.u.adjustdom.u.bvt;
 
     op.cmd = DOM0_ADJUSTDOM;
@@ -72,7 +72,7 @@ int xc_bvtsched_domain_get(int xc_handle,
                            long long *warpu)
 {
     
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     int ret;
     struct bvt_adjdom *adjptr = &op.u.adjustdom.u.bvt;
 
index e678769cdf7b9a45a730b084ddcaa01389240eee..062006094d31eb98eb47447103b950484a4130f2 100644 (file)
@@ -15,7 +15,7 @@ int xc_domain_create(int xc_handle,
                      uint32_t *pdomid)
 {
     int err;
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
 
     op.cmd = DOM0_CREATEDOMAIN;
     op.u.createdomain.domain = (domid_t)*pdomid;
@@ -32,7 +32,7 @@ int xc_domain_create(int xc_handle,
 int xc_domain_pause(int xc_handle, 
                     uint32_t domid)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_PAUSEDOMAIN;
     op.u.pausedomain.domain = (domid_t)domid;
     return do_dom0_op(xc_handle, &op);
@@ -42,7 +42,7 @@ int xc_domain_pause(int xc_handle,
 int xc_domain_unpause(int xc_handle,
                       uint32_t domid)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_UNPAUSEDOMAIN;
     op.u.unpausedomain.domain = (domid_t)domid;
     return do_dom0_op(xc_handle, &op);
@@ -52,7 +52,7 @@ int xc_domain_unpause(int xc_handle,
 int xc_domain_destroy(int xc_handle,
                       uint32_t domid)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_DESTROYDOMAIN;
     op.u.destroydomain.domain = (domid_t)domid;
     return do_dom0_op(xc_handle, &op);
@@ -63,7 +63,7 @@ int xc_domain_pincpu(int xc_handle,
                      int vcpu,
                      cpumap_t cpumap)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_PINCPUDOMAIN;
     op.u.pincpudomain.domain  = (domid_t)domid;
     op.u.pincpudomain.vcpu    = vcpu;
@@ -79,7 +79,7 @@ int xc_domain_getinfo(int xc_handle,
 {
     unsigned int nr_doms;
     uint32_t next_domid = first_domid;
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     int rc = 0; 
 
     memset(info, 0, max_doms*sizeof(xc_dominfo_t));
@@ -134,7 +134,7 @@ int xc_domain_getinfolist(int xc_handle,
                           xc_domaininfo_t *info)
 {
     int ret = 0;
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
 
     if ( mlock(info, max_domains*sizeof(xc_domaininfo_t)) != 0 )
         return -1;
@@ -161,7 +161,7 @@ int xc_domain_get_vcpu_context(int xc_handle,
                                vcpu_guest_context_t *ctxt)
 {
     int rc;
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
 
     op.cmd = DOM0_GETVCPUCONTEXT;
     op.u.getvcpucontext.domain = (domid_t)domid;
@@ -187,7 +187,7 @@ int xc_shadow_control(int xc_handle,
                       xc_shadow_control_stats_t *stats )
 {
     int rc;
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_SHADOW_CONTROL;
     op.u.shadow_control.domain = (domid_t)domid;
     op.u.shadow_control.op     = sop;
@@ -250,7 +250,7 @@ int xc_domain_setmaxmem(int xc_handle,
                         uint32_t domid, 
                         unsigned int max_memkb)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_SETDOMAINMAXMEM;
     op.u.setdomainmaxmem.domain = (domid_t)domid;
     op.u.setdomainmaxmem.max_memkb = max_memkb;
@@ -328,7 +328,7 @@ int xc_domain_memory_decrease_reservation(int xc_handle,
 
 int xc_domain_max_vcpus(int xc_handle, uint32_t domid, unsigned int max)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_MAX_VCPUS;
     op.u.max_vcpus.domain = (domid_t)domid;
     op.u.max_vcpus.max    = max;
@@ -338,7 +338,7 @@ int xc_domain_max_vcpus(int xc_handle, uint32_t domid, unsigned int max)
 int xc_domain_sethandle(int xc_handle, uint32_t domid, 
                         xen_domain_handle_t handle)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_SETDOMAINHANDLE;
     op.u.setdomainhandle.domain = (domid_t)domid;
     memcpy(op.u.setdomainhandle.handle, handle, sizeof(xen_domain_handle_t));
@@ -351,8 +351,7 @@ int xc_domain_get_vcpu_info(int xc_handle,
                             xc_vcpuinfo_t *info)
 {
     int rc;
-    dom0_op_t op;
-
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_GETVCPUINFO;
     op.u.getvcpuinfo.domain = (domid_t)domid;
     op.u.getvcpuinfo.vcpu   = (uint16_t)vcpu;
@@ -370,7 +369,7 @@ int xc_domain_ioport_permission(int xc_handle,
                                 uint16_t nr_ports,
                                 uint16_t allow_access)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
 
     op.cmd = DOM0_IOPORT_PERMISSION;
     op.u.ioport_permission.domain = (domid_t)domid;
index af79ef384a3bf9e9f55ac74a1aa86d105838c98c..ffc252ed5a3da19ff636e359df244a64805f01e0 100644 (file)
@@ -12,7 +12,7 @@
 static int do_evtchn_op(int xc_handle, evtchn_op_t *op)
 {
     int ret = -1;
-    privcmd_hypercall_t hypercall;
+    DECLARE_HYPERCALL;
 
     hypercall.op     = __HYPERVISOR_event_channel_op;
     hypercall.arg[0] = (unsigned long)op;
index fd9df4c1b2b62f7a7ac8635db2fd2d57fbda46e4..54fb40e6e142dba86944c152325c71d6aecb124c 100644 (file)
@@ -17,7 +17,7 @@ do_gnttab_op(int xc_handle,
              unsigned long count)
 {
     int ret = -1;
-    privcmd_hypercall_t hypercall;
+    DECLARE_HYPERCALL;
 
     hypercall.op     = __HYPERVISOR_grant_table_op;
     hypercall.arg[0] = cmd;
index cb72db04d6424f2f01ba2237a0b1f10ebd10ed0b..d61ecfa549cb17ce8480066c6755ac6b0e0435a1 100644 (file)
@@ -692,7 +692,8 @@ int xc_linux_build(int xc_handle,
                    unsigned int console_evtchn,
                    unsigned long *console_mfn)
 {
-    dom0_op_t launch_op, op;
+    dom0_op_t launch_op;
+    DECLARE_DOM0_OP;
     int initrd_fd = -1;
     gzFile initrd_gfd = NULL;
     int rc, i;
@@ -728,6 +729,10 @@ int xc_linux_build(int xc_handle,
         }
     }
 
+#ifdef VALGRIND
+    memset(&st_ctxt, 0, sizeof(st_ctxt));
+#endif
+
     if ( mlock(&st_ctxt, sizeof(st_ctxt) ) )
     {   
         PERROR("%s: ctxt mlock failed", __func__);
index 88c09c797c126b89931a676b13fc02f0cab0e4eb..20dfde7d98ea6cff2eb99cfafa6c80659160a898 100644 (file)
@@ -106,7 +106,7 @@ int xc_linux_restore(int xc_handle, int io_fd,
                      unsigned int store_evtchn, unsigned long *store_mfn,
                      unsigned int console_evtchn, unsigned long *console_mfn)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     int rc = 1, i, n;
     unsigned long mfn, pfn; 
     unsigned int prev_pc, this_pc;
index b0987d2a4733e384f213948284a40b72084929c3..13848fbf5f3d23788374c2064b1ca307aa790dc0 100644 (file)
@@ -25,7 +25,7 @@ int xc_readconsolering(int xc_handle,
                        int clear)
 {
     int ret;
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     char *buffer = *pbuffer;
     unsigned int nr_chars = *pnr_chars;
 
@@ -52,7 +52,7 @@ int xc_physinfo(int xc_handle,
                 xc_physinfo_t *put_info)
 {
     int ret;
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     
     op.cmd = DOM0_PHYSINFO;
     op.interface_version = DOM0_INTERFACE_VERSION;
@@ -69,7 +69,7 @@ int xc_sched_id(int xc_handle,
                 int *sched_id)
 {
     int ret;
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     
     op.cmd = DOM0_SCHED_ID;
     op.interface_version = DOM0_INTERFACE_VERSION;
@@ -83,25 +83,25 @@ int xc_sched_id(int xc_handle,
 }
 
 int xc_perfc_control(int xc_handle,
-                     uint32_t op,
+                     uint32_t opcode,
                      xc_perfc_desc_t *desc)
 {
     int rc;
-    dom0_op_t dop;
+    DECLARE_DOM0_OP;
 
-    dop.cmd = DOM0_PERFCCONTROL;
-    dop.u.perfccontrol.op   = op;
-    dop.u.perfccontrol.desc = desc;
+    op.cmd = DOM0_PERFCCONTROL;
+    op.u.perfccontrol.op   = opcode;
+    op.u.perfccontrol.desc = desc;
 
-    rc = do_dom0_op(xc_handle, &dop);
+    rc = do_dom0_op(xc_handle, &op);
 
-    return (rc == 0) ? dop.u.perfccontrol.nr_counters : rc;
+    return (rc == 0) ? op.u.perfccontrol.nr_counters : rc;
 }
 
 long long xc_msr_read(int xc_handle, int cpu_mask, int msr)
 {
     int rc;    
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     
     op.cmd = DOM0_MSR;
     op.u.msr.write = 0;
@@ -117,7 +117,7 @@ int xc_msr_write(int xc_handle, int cpu_mask, int msr, unsigned int low,
                   unsigned int high)
 {
     int rc;    
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     
     op.cmd = DOM0_MSR;
     op.u.msr.write = 1;
index 526d77b337802759c45223d09847f9bab359ae66..2c7759336ed94d3cdfdb3313651869374d745d9c 100644 (file)
@@ -68,7 +68,7 @@ void *xc_map_foreign_range(int xc_handle, uint32_t dom,
 int xc_get_pfn_type_batch(int xc_handle, 
                           uint32_t dom, int num, unsigned long *arr)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_GETPAGEFRAMEINFO2;
     op.u.getpageframeinfo2.domain = (domid_t)dom;
     op.u.getpageframeinfo2.num    = num;
@@ -81,7 +81,7 @@ unsigned int get_pfn_type(int xc_handle,
                           unsigned long mfn, 
                           uint32_t dom)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_GETPAGEFRAMEINFO;
     op.u.getpageframeinfo.pfn    = mfn;
     op.u.getpageframeinfo.domain = (domid_t)dom;
@@ -99,7 +99,7 @@ int xc_mmuext_op(
     unsigned int nr_ops,
     domid_t dom)
 {
-    privcmd_hypercall_t hypercall;
+    DECLARE_HYPERCALL;
     long ret = -EINVAL;
 
     hypercall.op     = __HYPERVISOR_mmuext_op;
@@ -125,7 +125,7 @@ int xc_mmuext_op(
 static int flush_mmu_updates(int xc_handle, xc_mmu_t *mmu)
 {
     int err = 0;
-    privcmd_hypercall_t hypercall;
+    DECLARE_HYPERCALL;
 
     if ( mmu->idx == 0 )
         return 0;
@@ -188,7 +188,7 @@ int xc_memory_op(int xc_handle,
                  int cmd,
                  void *arg)
 {
-    privcmd_hypercall_t hypercall;
+    DECLARE_HYPERCALL;
     struct xen_memory_reservation *reservation = arg;
     long ret = -EINVAL;
 
@@ -236,7 +236,7 @@ int xc_memory_op(int xc_handle,
 
 long long xc_domain_get_cpu_usage( int xc_handle, domid_t domid, int vcpu )
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
 
     op.cmd = DOM0_GETVCPUINFO;
     op.u.getvcpuinfo.domain = (domid_t)domid;
@@ -255,13 +255,16 @@ int xc_get_pfn_list(int xc_handle,
                     unsigned long *pfn_buf, 
                     unsigned long max_pfns)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     int ret;
     op.cmd = DOM0_GETMEMLIST;
     op.u.getmemlist.domain   = (domid_t)domid;
     op.u.getmemlist.max_pfns = max_pfns;
     op.u.getmemlist.buffer   = pfn_buf;
 
+#ifdef VALGRIND
+    memset(pfn_buf, 0, max_pfns * sizeof(unsigned long));
+#endif
 
     if ( mlock(pfn_buf, max_pfns * sizeof(unsigned long)) != 0 )
     {
@@ -293,7 +296,7 @@ int xc_get_pfn_list(int xc_handle,
 
 long xc_get_tot_pages(int xc_handle, uint32_t domid)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     op.cmd = DOM0_GETDOMAININFO;
     op.u.getdomaininfo.domain = (domid_t)domid;
     return (do_dom0_op(xc_handle, &op) < 0) ? 
@@ -403,6 +406,11 @@ int xc_version(int xc_handle, int cmd, void *arg)
         return -ENOMEM;
     }
 
+#ifdef VALGRIND
+    if (argsize != 0)
+        memset(arg, 0, argsize);
+#endif
+
     rc = do_xen_version(xc_handle, cmd, arg);
 
     if ( argsize != 0 )
index 0e20d2db59c770bc3a69b50df1075ab7d35410cc..2a3164c1ac442a76b2a3985c6b60f544b8942a57 100644 (file)
 
 #include <xen/linux/privcmd.h>
 
+/* valgrind cannot see when a hypercall has filled in some values.  For this
+   reason, we must zero the privcmd_hypercall_t or dom0_op_t instance before a
+   call, if using valgrind.  */
+#ifdef VALGRIND
+#define DECLARE_HYPERCALL privcmd_hypercall_t hypercall; \
+  memset(&hypercall, 0, sizeof(hypercall))
+#define DECLARE_DOM0_OP dom0_op_t op; memset(&op, 0, sizeof(op))
+#else
+#define DECLARE_HYPERCALL privcmd_hypercall_t hypercall
+#define DECLARE_DOM0_OP dom0_op_t op
+#endif
+
+
 #define PAGE_SHIFT              XC_PAGE_SHIFT
 #define PAGE_SIZE               (1UL << PAGE_SHIFT)
 #define PAGE_MASK               (~(PAGE_SIZE-1))
@@ -61,7 +74,7 @@ static inline int do_xen_hypercall(int xc_handle,
 
 static inline int do_xen_version(int xc_handle, int cmd, void *dest)
 {
-    privcmd_hypercall_t hypercall;
+    DECLARE_HYPERCALL;
 
     hypercall.op     = __HYPERVISOR_xen_version;
     hypercall.arg[0] = (unsigned long) cmd;
@@ -73,7 +86,7 @@ static inline int do_xen_version(int xc_handle, int cmd, void *dest)
 static inline int do_dom0_op(int xc_handle, dom0_op_t *op)
 {
     int ret = -1;
-    privcmd_hypercall_t hypercall;
+    DECLARE_HYPERCALL;
 
     op->interface_version = DOM0_INTERFACE_VERSION;
 
index 5bddbbff1f164ef5c6c4577b9bc8eb12cb9753bb..c101d3225ba1116f4441e56c96cfe4ba599affdb 100644 (file)
@@ -283,7 +283,7 @@ xc_waitdomain(
     int *status,
     int options)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     int retval;
     struct timespec ts;
     ts.tv_sec = 0;
@@ -323,7 +323,7 @@ xc_ptrace(
     long eaddr,
     long edata)
 {
-    dom0_op_t       op;
+    DECLARE_DOM0_OP;
     int             status = 0;
     struct gdb_regs pt;
     long            retval = 0;
index 64984afa12026ca4afbf69f69bb3294e0d15ded5..34216ed97ec762ef7b09cf8dcb1a266236b5ec8d 100644 (file)
@@ -13,7 +13,7 @@
 int xc_sedf_domain_set(int xc_handle,
                           uint32_t domid, uint64_t period, uint64_t slice,uint64_t latency, uint16_t extratime,uint16_t weight)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     struct sedf_adjdom *p = &op.u.adjustdom.u.sedf;
 
     op.cmd = DOM0_ADJUSTDOM;
@@ -31,7 +31,7 @@ int xc_sedf_domain_set(int xc_handle,
 
 int xc_sedf_domain_get(int xc_handle, uint32_t domid, uint64_t *period, uint64_t *slice, uint64_t* latency, uint16_t* extratime, uint16_t* weight)
 {
-    dom0_op_t op;
+    DECLARE_DOM0_OP;
     int ret;
     struct sedf_adjdom *p = &op.u.adjustdom.u.sedf;
 
index b47cde286756b55a6dd0ff1a56c9221e05f0ed71..b9a5e51a01af3a1595d7e6f48ec4295458652097 100644 (file)
@@ -10,7 +10,7 @@
 
 int xc_tbuf_enable(int xc_handle, int enable)
 {
-  dom0_op_t op;
+  DECLARE_DOM0_OP;
 
   op.cmd = DOM0_TBUFCONTROL;
   op.interface_version = DOM0_INTERFACE_VERSION;
@@ -24,7 +24,7 @@ int xc_tbuf_enable(int xc_handle, int enable)
 
 int xc_tbuf_set_size(int xc_handle, uint32_t size)
 {
-  dom0_op_t op;
+  DECLARE_DOM0_OP;
 
   op.cmd = DOM0_TBUFCONTROL;
   op.interface_version = DOM0_INTERFACE_VERSION;
@@ -37,7 +37,7 @@ int xc_tbuf_set_size(int xc_handle, uint32_t size)
 int xc_tbuf_get_size(int xc_handle, uint32_t *size)
 {
   int rc;
-  dom0_op_t op;
+  DECLARE_DOM0_OP;
 
   op.cmd = DOM0_TBUFCONTROL;
   op.interface_version = DOM0_INTERFACE_VERSION;
index ab0b188ffc64f1a7b6285695a00b6534dd1a0b71..a2dac8b0f31be3d4e7ecdda3080170565bda582f 100644 (file)
 #include <xen/linux/privcmd.h>
 #include <xen/memory.h>
 
+/* valgrind cannot see when a hypercall has filled in some values.  For this
+   reason, we must zero the dom0_op_t instance before a call, if using
+   valgrind.  */
+#ifdef VALGRIND
+#define DECLARE_DOM0_OP dom0_op_t op; memset(&op, 0, sizeof(op))
+#else
+#define DECLARE_DOM0_OP dom0_op_t op
+#endif
+
+
 char *xc_read_kernel_image(const char *filename, unsigned long *size);
 unsigned long csum_page (void * page);